package fr.xgouchet.wedding.client.presenter;

import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author x.gouchet
 * 
 */
public class FeedbackMessagePresenter implements Presenter {
	/**
	 * The display interface that the binded view needs to implement.
	 * 
	 * @author x.gouchet
	 * 
	 */
	public interface Display {
		/**
		 * @param data
		 *            the string to display
		 */
		void setData(String data);

		/**
		 * @param error
		 *            additional info to present the text as an error or not
		 */
		void setError(boolean error);

		/**
		 * @return the view as a single widget
		 */
		Widget asWidget();
	}

	/**
	 * Constructor
	 * 
	 * @param view
	 *            the view to present
	 * @param msg
	 *            the message to present
	 * @param error
	 *            the style of the message
	 */
	public FeedbackMessagePresenter(Display view, String msg, boolean error) {
		mDisplay = view;
		mMessage = msg;
		mError = error;
	}

	/**
	 * @see Presenter#go(HasWidgets)
	 */
	public void go(final HasWidgets container) {
		container.clear();
		container.add(mDisplay.asWidget());
		if ((mMessage == null) || (mMessage.length() == 0))
			return;
		mDisplay.setData(mMessage);
		mDisplay.setError(mError);
	}

	/** The display view linked with this presenter */
	private final Display mDisplay;

	/** The message to feed */
	private final String mMessage;

	/** is the message an error */
	private final boolean mError;
}
